<script language="javascript" >
<!--

var parentOpts = new Array();
var curOpts = new Array();
var tmpText = new String;
var tmpValue = new String;
var isInherit = <%=bInherit%>;
var isFirst = true;
parentOpts.length =
<%
  if ((parentPermissions != null && parentPermissions.size() >0))
     out.print(parentPermissions.size());
  else
     out.print("0");
  out.print(";");
  Iterator it = parentPermissions.iterator();
  for (int i = 0; i < parentPermissions.size(); i++)
  {
    PermissionEntry permission = (PermissionEntry)it.next();
    if (permission != null)
    {
      if (inheritEntryName == null)
        inheritEntryName = home.getPathString(permission.getObjectid());
      StringBuffer strValue = new StringBuffer();
      strValue.append(permission.getUserid());

      if (permission.getIsgroup())
      {
        strValue.append("1");
      }
      else
      {
        strValue.append("0");
      }
      out.print("tmpText=\"\";");
      out.print("tmpValue=\"");
      out.print(strValue);
      out.println("\";");
      out.println("isFirst = true; ");
      out.print("isFirst = formatPermission(");
      out.print((permission.getPermission() & home.BROWSE_ALLOW) != 0);
      out.print(",");
      out.print((permission.getPermission() & home.BROWSE_DENY) != 0);
      out.println(",\"Browse\",isFirst);");

      out.print("isFirst = formatPermission(");
      out.print((permission.getPermission() & home.READ_ALLOW) != 0);
      out.print(",");
      out.print((permission.getPermission() & home.READ_DENY) != 0);
      out.println(",\"Read\",isFirst);");

      out.print("isFirst = formatPermission(");
      out.print((permission.getPermission() & home.WRITE_ALLOW) != 0);
      out.print(",");
      out.print((permission.getPermission() & home.WRITE_DENY) != 0);
      out.println(",\"Write\",isFirst);");

      out.print("isFirst = formatPermission(");
      out.print((permission.getPermission() & home.TAKEOWNERSHIP_ALLOW) != 0);
      out.print(",");
      out.print((permission.getPermission() & home.TAKEOWNERSHIP_DENY) != 0);
      out.println(",\"Take Ownership\",isFirst);");

      out.print("parentOpts[");
      out.print(i);
      out.print("] = new Option(\"");
      out.print(home.getPermissionString(permission));
      out.println("\",tmpValue);");
    }
  }
%>
curOpts.length =
<%
  if ((curPermissions != null && curPermissions.size() >0))
     out.print(curPermissions.size());
  else
     out.print("0");
  out.print(";");
  Iterator itCur = curPermissions.iterator();
  for (int i = 0; i < curPermissions.size(); i++)
  {
    PermissionEntry permission = (PermissionEntry)itCur.next();
    if (permission != null)
    {
      if (inheritEntryName == null)
        inheritEntryName = home.getPathString(permission.getObjectid());
      StringBuffer strValue = new StringBuffer();
      strValue.append(permission.getUserid());

      if (permission.getIsgroup())
      {
        strValue.append("1");
      }
      else
      {
        strValue.append("0");
      }
      out.print("tmpText=\"\";");
      out.print("tmpValue=\"");
      out.print(strValue);
      out.println("\";");
      out.println("isFirst = true; ");
      out.print("isFirst = formatPermission(");
      out.print((permission.getPermission() & home.BROWSE_ALLOW) != 0);
      out.print(",");
      out.print((permission.getPermission() & home.BROWSE_DENY) != 0);
      out.println(",\"Browse\",isFirst);");

      out.print("isFirst = formatPermission(");
      out.print((permission.getPermission() & home.READ_ALLOW) != 0);
      out.print(",");
      out.print((permission.getPermission() & home.READ_DENY) != 0);
      out.println(",\"Read\",isFirst);");

      out.print("isFirst = formatPermission(");
      out.print((permission.getPermission() & home.WRITE_ALLOW) != 0);
      out.print(",");
      out.print((permission.getPermission() & home.WRITE_DENY) != 0);
      out.println(",\"Write\",isFirst);");

      out.print("isFirst = formatPermission(");
      out.print((permission.getPermission() & home.TAKEOWNERSHIP_ALLOW) != 0);
      out.print(",");
      out.print((permission.getPermission() & home.TAKEOWNERSHIP_DENY) != 0);
      out.println(",\"Take Ownership\",isFirst);");

      out.print("curOpts[");
      out.print(i);
      out.print("] = new Option(\"");
      out.print(home.getPermissionString(permission));
      out.println("\",tmpValue);");
    }
  }
%>
//alert("before calling resetPermissions(false)");

SafeAddOnload(newOnLoad);
//window.onload = "newOnLoad()";
function newOnLoad()
{
  resetPermissions(false);
}

function resetPermissions(isParent)
{
  if (isParent)
  {
    document.forms[0].PERMISSIONS.length = parentOpts.length;
    for(var i = 0; i < parentOpts.length; i++)
    {
      document.forms[0].PERMISSIONS[i] = parentOpts[i];
    }
  }
  else
  {
//alert("in calling resetPermissions(false)");
//alert(curOpts.length);
    document.forms[0].PERMISSIONS.length = curOpts.length;
//alert(PERMISSIONS.length);
    for(var i = 0; i < curOpts.length; i++)
    {
//alert(curOpts[i].text);
      document.forms[0].PERMISSIONS[i] = curOpts[i];
    }
  }
}

function INHERIT_onclick()
{
// the checked value has not been reflected yet
  if (isInherit)
  {
    isInherit = false;
//    document.forms[0].INHERIT.checked = false;

    document.forms[0].ADD.disabled = false;
    document.forms[0].REMOVE.disabled = false;
  }
  else
  {
    if (confirm("Choose inherit from parent will lose your current permission setting"))
    {
      isInherit = true;
      document.forms[0].ADD.disabled = true;
      document.forms[0].REMOVE.disabled = true;
      resetPermissions(true);
    }
    else
      return false;
  }
}
function formatPermission(allow, deny, permission, first)
{
  if (allow)
  {
    tmpValue += "01";
    if (!first)
      tmpText+= "/";
    tmpText += "Allow ";
    tmpText += permission;
    first = false;
  }
  else if (deny)
  {
    tmpValue += "10";
    if (!first)
      tmpText+= "/";
    tmpText += "Deny ";
    tmpText += permission;
    first = false;
  }
 else
    tmpValue += "00";

  return first;
}

function ADD_onclick()
{
  if (document.forms[0].USERS.selectedIndex == -1)
  {
    alert("You must select one user or group");
    return;
  }
  if (document.forms[0].USERS.options[document.forms[0].USERS.selectedIndex].value == -1)
  {
    alert("You must select one user or group");
    return;
  }

  var curUser = document.forms[0].USERS.options[document.forms[0].USERS.selectedIndex].value;
//alert(curUser);
  var matchStr = "\\b"+curUser;
//alert(matchStr);
  for(var i = 0; i < document.forms[0].PERMISSIONS.length; i++)
  {
//    alert(PERMISSIONS.options[i].value);
    if (document.forms[0].PERMISSIONS.options[i].value.match(matchStr))
    {
      var curUserName = document.forms[0].USERS.options[document.forms[0].USERS.selectedIndex].text;
//      curUserName = curUserName.substr(curUserName.indexOf("-")+1, curUserName.indexOf(" : "));
      alert("You have "+ curUserName + " in the list already, remove it first!");
      return;
    }
  }

  if (document.forms[0].BROWSE[0].checked &&
  document.forms[0].READ[0].checked &&
  document.forms[0].WRITE[0].checked &&
  document.forms[0].TAKEOWNERSHIP[0].checked )
  {
    alert("You must select at lease one permission");
    return;
  }
//  alert("Start working");
//  alert(curUser);

  //The value format to be added in the listbox is
  //userID(?) + GroupFlag(1) + permissions(8)
  tmpValue = curUser;
  if (curUser.charAt(curUser.length-1) == '0')
    tmpText = "User-";
  else
    tmpText = "Group-";
  tmpText += document.forms[0].USERS.options[document.forms[0].USERS.selectedIndex].text;
  tmpText += " : ";
  isFirst = true;
  isFirst = formatPermission(document.forms[0].TAKEOWNERSHIP[1].checked, document.forms[0].TAKEOWNERSHIP[2].checked, "Take Ownership", isFirst);
  isFirst = formatPermission(document.forms[0].BROWSE[1].checked, document.forms[0].BROWSE[2].checked, "Browse", isFirst);
  isFirst = formatPermission(document.forms[0].WRITE[1].checked, document.forms[0].WRITE[2].checked, "Write", isFirst);
  isFirst = formatPermission(document.forms[0].READ[1].checked, document.forms[0].READ[2].checked, "Read", isFirst);
//  alert(tmpText);
//  alert(tmpValue);
//  alert(PERMISSIONS.length);

  var option = new Option(tmpText, tmpValue);
  document.forms[0].PERMISSIONS.options[document.forms[0].PERMISSIONS.length] = option;
}

function REMOVE_onclick()
{
  if ( document.forms[0].PERMISSIONS.length > 0)
  {
    for (i=document.forms[0].PERMISSIONS.length-1; i >=0; i--)
    {
      if (document.forms[0].PERMISSIONS[i].selected == true)
       document.forms[0].PERMISSIONS[i] = null;
    }
  }
}

//select all the items so they will be sent out
function onSubmit()
{
  if ( document.forms[0].PERMISSIONS.options.length > 0)
  {
    for (i=document.forms[0].PERMISSIONS.options.length-1; i >=0; i--)
    {
      document.forms[0].PERMISSIONS.options[i].selected = true;
    }
  }
}
//-->
    </script>

    <TR bgcolor="#7EC1F3">
      <TD colSpan=2> <b><font size="+1">Security:</font></b>
      </TD>
    </TR>
   <tr>
     <td>
<%
              if (entry.getOwnerid() != curID && home.checkAccessRights(curID, entry, home.TAKEOWNERSHIP_ALLOW))
                out.print("<INPUT TYPE=\"checkbox\" name=\"TAKEOWNERSHIP\" value=1>Take ownership");
%>
     </td>
   </tr>
    <tr>
      <td><font size="+1"><b>
	<INPUT language="javascript" id="INHERIT" value="1" onclick="return INHERIT_onclick()"
        type="checkbox" name="INHERIT"
        <%
          if (bInherit)
            out.print(" CHECKED ");
          if (entry.getID() == home.ROOT)
            out.print(" disabled ");
        %>
        >Inherit from
        <%
          if (inheritEntryName != null)
            out.print(inheritEntryName);
          else
            out.print("parent");
        %></b></font>
    <tr>
      <td>Current permissions:</b></td>
    </tr>
    <tr>
      <td><SELECT id="PERMISSIONS" multiple size="8" name="PERMISSIONS" >
      </SELECT>  </td>
    </tr>
    <tr>
      <td>To remove permissions, select items in the list and click&nbsp;<INPUT language="javascript" id="REMOVE"  onclick="return REMOVE_onclick()" type="button"
        value="Remove selected permissions" name="REMOVE" <%  if (bInherit) out.print(" disabled ");  %>    >
      </td>
    </tr>
    <TR  bgcolor="#7EC1F3" >
      <TD> <font size="+1"><b>Add Permissions
        </b></font>
      </TD>
    </TR>
    <tr>
      <td>
    To Add user/group permission, select the user/group from the following list,
    select the permissions, and click
      </td>
    </tr>
    <tr>
      <td>
         <INPUT language="javascript" id="ADD"  onclick="return ADD_onclick()" type="button"
        value="Add Permission" name="Add"  <%  if (bInherit) out.print(" disabled ");  %>  >
      </td>
    </tr>
    <tr>
      <td>
      Select User/Group : &nbsp;
      </td>
    </tr>
    <tr>
      <td>
        <SELECT id="USERS" name="USERS">
          <OPTION value="-1" selected>Users:</OPTION>
<%
            int[] ids = um.getAllUsers(" order by lastname, firstname");
            if (ids != null && ids.length >0)
            {
              for (int i = 0; i < ids.length; i++)
              {
                if (entry.getOwnerid() != ids[i])
                {
                  out.print("<option value=\"");
                  out.print(ids[i]);
                  out.print("0\">");
                  out.print(um.getUserDisplayName(ids[i], um.USERNAME_FORMAT_ALL));
                  out.println("</option>");
                }
              }
            }
%>
          <OPTION value="-1" >Groups:</OPTION>
<%
            Collection grpCol = um.getAllUserGrps(" order by name");
            if (grpCol != null && grpCol.size() >0)
            {
              Iterator grpIt = grpCol.iterator();
              while (grpIt.hasNext())
              {
                UserGrpKey grpKey = (UserGrpKey) grpIt.next();
                if (grpKey != null)
                {
                  UserGrpObject grp = um.getUserGrp(grpKey);
                  if (grp != null)
                  {
                    out.print("<option value=\"");
                    out.print(grp.getID());
                    out.print("1\">");
                    out.print(grp.getName());
                    out.println("</option>");
                  }
                }
              }
            }
%>

        </SELECT>
      </td>
    </tr>
    <tr>
      <td>
      Choose the permissions for selected User/Group:
      </td>
    </tr>
    <tr>
      <td>
        <TABLE id="Sec" cellSpacing="1" cellPadding="1" border="1">
          <TR>
            <TD style="WIDTH: 109px"> &nbsp;</TD>
            <TD align="center">None</TD>
            <TD align="center">Allow</TD>
            <TD align="center">Deny</TD>
            <TD>&nbsp;</TD>
          </TR>
          <TR>
            <TD style="WIDTH: 109px">Browse</TD>
            <TD align="center"><INPUT id="BROWSE" type="radio" CHECKED value="Radio4" name="BROWSE"></TD>
            <TD align="center"><INPUT id="BROWSE" type="radio" value="Radio5" name="BROWSE"></TD>
            <TD align="center"><INPUT id="BROWSE" type="radio" value="Radio6" name="BROWSE"></TD>
            <td>
              The <b>Browse</b>permission allows user to view the attributes of the object, user can see the object exists in the library.
            </td>
          </TR>
          <TR>
            <TD style="WIDTH: 109px">Read</TD>
            <TD align="center"><INPUT id="READ" type="radio" CHECKED value="Radio4" name="READ"></TD>
            <TD align="center"><INPUT id="READ" type="radio" value="Radio4" name="READ"></TD>
            <TD align="center"><INPUT id="READ" type="radio" value="Radio4" name="READ"></TD>
            <td>
              The <b>Read</b> permission allows user to download the object. Only useful for file.
            </td>
          </TR>
          <TR>
            <TD style="WIDTH: 109px">Write</TD>
            <TD align="center"><INPUT id="WRITE" type="radio" CHECKED value="Radio4" name="WRITE"></TD>
            <TD align="center"><INPUT id="WRITE" type="radio" value="Radio4" name="WRITE"></TD>
            <TD align="center"><INPUT id="WRITE" type="radio" value="Radio4" name="WRITE"></TD>
            <td>
              The <b>Write</b> permission allows add/delete/update/change permissions on an object, useful for both folder/file.
            </td>
          </TR>
          <TR>
            <TD style="WIDTH: 109px">Take Ownership</TD>
            <TD align="center"><INPUT id="TAKEOWNERSHIP" type="radio" CHECKED value="Radio4" name="TAKEOWNERSHIP"></TD>
            <TD align="center"><INPUT id="TAKEOWNERSHIP" type="radio" value="Radio4" name="TAKEOWNERSHIP"></TD>
            <TD align="center"><INPUT id="TAKEOWNERSHIP" type="radio" value="Radio4" name="TAKEOWNERSHIP"></TD>
            <td>
              The <b>Take Ownership</b> permission allows user to take the ownership of the object. The owner of the object can do anything on the object.
            </td>
          </TR>
        </TABLE>
      </td>
    </tr>
